package cn.hou.java.designpattern._07observerself;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

//气象站类:当天气变化时,通知人
public class WetherStation {
	String wether;//天气
	String[] wethers={"晴天","雨天","雾霾"};
	//收听天气人的集合
	List<Person> list=new ArrayList<Person>();
	//添加听众
	public void addListener(Person person) {
		list.add(person);
	}
	//天气变化
	public void updateWether(){
		//随机变化天气
		wether=wethers[new Random().nextInt(wethers.length)];
		System.out.println("当前天气是:"+wether);
	}
	//气象站开始工作
	public void startWork(){
		new Thread(()->{
			while(true){
				try {
					updateWether();//天气变化
					for (Person person : list) {
						//通知每一个天气变化了
						person.todoByWether(wether);
					}
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

}
